package com.sskj.hangqing.ui.fragment;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;

import com.sskj.hangqing.R2;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.hangqing.R;
import com.sskj.hangqing.bean.PushTradeBean;
import com.sskj.hangqing.bean.TradeBean;
import com.sskj.hangqing.bean.rxbus.MarketCoinBean;
import com.sskj.hangqing.presenter.TradeFragmentPresenter;
import com.sskj.lib.bean.BuySellEnum;
import com.sskj.lib.util.CoinUtil;
import com.sskj.lib.util.CommonUtil;
import com.sskj.lib.util.NumberUtil;
import com.sskj.lib.util.TimeFormatUtil;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;

@Route(path = RConfig.APP_FRAGMENT_TRADE)
public class TradeFragment extends BaseFragment<TradeFragmentPresenter> {
    @BindView(R2.id.tvTime)
    TextView tvTime;
    @BindView(R2.id.tvDirection)
    TextView tvDirection;
    @BindView(R2.id.tvPrice)
    TextView tvPrice;
    @BindView(R2.id.tvNum)
    TextView tvNum;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;

    @Autowired
    String code;
    private SlimAdapter slimAdapter;


    @Override
    protected int getLayoutId() {
        return R.layout.app_fragment_trade;
    }

    @Override
    public TradeFragmentPresenter getPresenter() {
        return new TradeFragmentPresenter();
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        RxBus.getDefault().register(this);
        initRecy();
    }

    @Override
    public void initData() {
        mPresenter.getData(code);
        mPresenter.initSocket(code);
    }

    private void initRecy() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        slimAdapter = SlimAdapter.create().register(R.layout.app_recy_item_trade, new SlimInjector<TradeBean>() {
            @Override
            public void onInject(TradeBean data, IViewInjector injector, List payloads) {
                injector.text(R.id.tvTime, data.getTime());
                if (data.getCj_type().equals("1")) {
                    injector.text(R.id.tvDirection, BuySellEnum.BUY.getTitle());
                } else {
                    injector.text(R.id.tvDirection, BuySellEnum.SELL.getTitle());
                }
                injector.textColor(R.id.tvDirection, ContextCompat.getColor(App.INSTANCE, data.getCj_type().equals("1") ?
                        BuySellEnum.BUY.getColorRes() : BuySellEnum.SELL.getColorRes()));
                injector.text(R.id.tvPrice, data.getPrice());
                injector.text(R.id.tvNum, data.getNum());
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void changeCoin(MarketCoinBean marketCoinBean) {
        code = marketCoinBean.getCode();
        if (tvNum == null)
            return;
        String replace = code.replace("_", "/");
        mPresenter.getData(code);
        mPresenter.sendCode(code);
    }

    public void updateUI(List<TradeBean> data) {
        if (slimAdapter == null)
            return;
        slimAdapter.updateData(data);
    }

    @Override
    public void onDestroy() {
        mPresenter.closeWebSocket();
        super.onDestroy();
    }

    public void updateData(PushTradeBean data) {
        if (data.getCode().equalsIgnoreCase(code)) {
            Flowable.fromIterable(data.getData())
                    .take(20)
                    .map(dataBean -> new TradeBean(
                            TimeFormatUtil.SF_FORMAT_H.format(new Date(dataBean.getDt())),
                            dataBean.getDc().equals("buy") ? "1" : "2",
                            CoinUtil.keepCoinPrice(code, dataBean.getPrice() + ""),
                            dataBean.getAmount() + ""
                    ))
                    .toList()
                    .subscribe((tradeBeans, throwable) -> {
                        slimAdapter.updateData(tradeBeans);
                    });

        }
    }
}
